package com.hongyuan.venue.ui.main.view_mode;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import com.hongyuan.venue.base.CustomViewModel;
import com.hongyuan.venue.data.Constants;
import com.hongyuan.venue.data.MyRepository;
import com.hongyuan.venue.data.md5.BaseUtil;
import com.hongyuan.venue.data.userbean.MemberLoginBean;
import com.hongyuan.venue.ui.main.activity.LoginPassWordActivity;
import com.hongyuan.venue.ui.main.activity.MainActivity;
import com.hongyuan.venue.ui.main.activity.WebActivity;
import com.hongyuan.venue.ui.main.beans.PhoneMessageTokenBean;
import com.hongyuan.venue.utils.HourMeterUtil;
import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;

public class CodeLoginViewModel extends CustomViewModel<MyRepository> {

    private HourMeterUtil meterUtil;

    private int nowSecond = 60;

    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //验证码的绑定
    public ObservableField<String> verificationCode = new ObservableField<>("");
    //验证码发生的文字描述
    public ObservableField<String> sendCodeText = new ObservableField<>("发送验证码");

    public CodeLoginViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //验证码发送点击
    public BindingCommand sendCode = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            if(nowSecond == 60){
                if(!BaseUtil.isValue(userName.get())){
                    ToastUtils.showShort("请输入手机号");
                    return;
                }
                setParams("m_mobile", userName.get());
                requestData(Constants.GET_MESSAGE_TOKEN);
            }

        }
    });

    //密码登录
    public BindingCommand goPasswordLogin = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(LoginPassWordActivity.class);
        }
    });
    //登录
    public BindingCommand submit = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            codeLogin();
        }
    });

    //隐私政策
    public BindingCommand goZhengCe = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("url","http://www.hongyuangood.com/xy/xy.html");
            bundle.putString("title","隐私政策");
            startActivity(WebActivity.class,bundle);
        }
    });


    @Override
    public void onCreate() {
        super.onCreate();

        meterUtil = new HourMeterUtil();
        meterUtil.setTimeCallBack(passedTime -> {
            if(passedTime > 0 && passedTime < 60){
                nowSecond = 60-passedTime;

                sendCodeText.set((60-passedTime)+"s");
            }else{
                if(passedTime >= 60){
                    nowSecond = 60;
                    meterUtil.stopCount();
                    sendCodeText.set("发送验证码");
                }
            }
        });
    }

    /*
    * 修改密码请求
    * */
    private void codeLogin(){
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }

        if (TextUtils.isEmpty(verificationCode.get())) {
            ToastUtils.showShort("请输入验证码！");
            return;
        }

        clearParams().setParams("m_mobile", userName.get())
                .setParams("dxm",verificationCode.get());

        //短信登录
        requestData(Constants.MEMBERLOGIN_DXM);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_MESSAGE_TOKEN){
            PhoneMessageTokenBean tokenBean = (PhoneMessageTokenBean)dataBean;
            //开始倒计时
            meterUtil.startCount();

            //获取短信验证码
            setParams("m_mobile",userName.get()).setParams("sms_checked_code",tokenBean.getToken());
            requestData(Constants.SEND_REGI_MSG);
        }

        if(code == Constants.MEMBERLOGIN_DXM){
            MemberLoginBean bean = (MemberLoginBean) dataBean;

            userToken.setM_id(bean.getM_id());
            userToken.setM_mobile(bean.getM_mobile());
            userToken.setRole_id(String.valueOf(bean.getRole_id()));

            //保存用户登录信息
            model.saveUser(bean);

            //极光推送设置别名或标签
            //JPushInterface.setAlias(getApplication().getApplicationContext(), 100, userToken.getM_mobile());

            startActivity(MainActivity.class);
            finish();

        }

    }
}
